package com.zhugang.week13.hash;

import java.util.HashSet;
import java.util.Set;

/**
 * @program algorithms
 * @description: intersection
 * @author: chanzhugang
 * @create: 2022/12/08 20:44
 */
public class Intersection {

    /**
     * 349、两个数组的交集
     * https://leetcode.cn/problems/intersection-of-two-arrays/
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        /**
         * 哈希集合存储其中一个数组
         */
        Set<Integer> set = new HashSet<>();
        for (int num : nums1) {
            set.add(num);
        }
        Set<Integer> result = new HashSet<>();
        for (int num : nums2) {
            if (set.contains(num)) {
                result.add(num);
            }
        }
        // 集合转数组
        return result.stream().mapToInt(i -> i).toArray();
    }
}